import 'package:cached_network_image/cached_network_image.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:x_core/x_core.dart';
import 'package:x_project/common/config.dart';

/// Created by XieXin on 2020/3/16.
/// 图片加载（支持本地与网络图片）
class LoadImage extends StatelessWidget {
  final String image;
  final double? width;
  final double? height;
  final BoxFit fit;
  final String format;
  final String holderImg;
  final String errorImg;
  final Widget? placeholder;
  final Widget? errorWidget;

  const LoadImage(
    this.image, {
    Key? key,
    this.width,
    this.height,
    this.fit = BoxFit.cover,
    this.format = "png",
    this.holderImg = "ic_default_image",
    this.errorImg = "ic_default_image",
    this.placeholder,
    this.errorWidget,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (TextUtil.isEmpty(image)) {
      return errorWidget ??
          LoadAssetImage(
            holderImg,
            height: height,
            width: width,
            fit: fit,
            format: format,
            moduleName: Config.getModuleName(),
          );
    } else {
      if (image.startsWith("http")) {
        return CachedNetworkImage(
            imageUrl: image,
            width: width,
            height: height,
            fit: fit,
            placeholder: (context, url) {
              return placeholder ??
                  LoadAssetImage(
                    holderImg,
                    height: height,
                    width: width,
                    fit: fit,
                    moduleName: Config.getModuleName(),
                  );
            },
            errorWidget: (context, url, error) {
              return errorWidget ??
                  LoadAssetImage(
                    errorImg,
                    height: height,
                    width: width,
                    fit: fit,
                    moduleName: Config.getModuleName(),
                  );
            });
      } else {
        return LoadAssetImage(
          image,
          height: height,
          width: width,
          fit: fit,
          format: format,
          moduleName: Config.getModuleName(),
        );
      }
    }
  }
}

/// Created by XieXin on 2020/3/16.
/// 加载本地资源图片
class LoadAssetImage extends StatelessWidget {
  final String image;
  final double? width;
  final double? height;
  final BoxFit? fit;
  final String moduleName;
  final String format;
  final Color? color;

  const LoadAssetImage(
    this.image, {
    Key? key,
    this.width,
    this.height,
    this.fit,
    this.moduleName = '',
    this.format = "png",
    this.color,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Image.asset(
      ImageUtils.getImgPath(image, moduleName: moduleName, format: format),
      height: height,
      width: width,
      fit: fit,
      color: color,
      gaplessPlayback: true,
      //原图片保持不变，直到图片加载完成时替换图片，这样就不会出现闪烁
      excludeFromSemantics: true, //忽略图片语义
    );
  }
}
